// google analytics compatibility
var _gaq = _gaq || [];

liwe.ga = {};

liwe.ga.init = function ( gaid )
{
	if ( typeof ( _gaq.length ) === 'undefined' ) return;

	// START GOOGLE ANALYTICS CODE
	_gaq.push ( ['_setAccount', gaid ] );

	var ga = document.createElement ( 'script' );
	ga.type = 'text/javascript'; ga.async = true;
	ga.src = ( 'https:' == document.location.protocol ? 'https://ssl' : 'http://www' ) + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName ( 'script' ) [ 0 ];
	s.parentNode.insertBefore ( ga, s );
	// END GOOGLE ANALYTICS
};

/* DEFAULTS FOR CUSTOM 
	slot: 
			1		user ( default )
	level:
			1		visitor
			2		session ( default )
			3		page 
*/
liwe.ga.custom = function ( key, value, slot, level )
{
	if ( ! window [ '_gaq' ] )
	{
		console.warn ( 'Google analytics not initialized!' );
		return;
	}
	/* required fields */
	slot = slot || 1;
	level = level || 2;
	_gaq.push ( [ '_setCustomVar', slot, key, value, level ] );
};

liwe.ga.del_custom = function ( slot )
{
	if ( ! window [ '_gaq' ] )
	{
		console.warn ( 'Google analytics not initialized!' );
		return;
	}
	slot = slot || 1;
	_gaq.push ( [ '_deleteCustomVar', slot ] );
};

// see https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide
liwe.ga.event = function ( category, action, label, value )
{
	if ( ! window [ '_gaq' ] )
	{
		console.warn ( 'Google analytics not initialized!' );
		return;
	}

	if ( typeof ( category ) === 'undefined' )
	{
		console.error ( 'liwe.ga: missing parameter: category' );
		return;
	}

	if ( typeof ( action ) === 'undefined' )
	{
		console.error ( 'liwe.ga: missing parameter: action' );
		return;
	}

	value = parseInt ( value, 10 );
	if ( isNaN ( value ) ) value = null;

	_gaq.push ( [ '_trackEvent', category, action, label, value ] );
};

liwe.ga.track = function ( url )
{
	_gaq.push ( ['_trackPageview', url ] );
};

//load google conversion js code
liwe.ga._init_conv = function ()
{
        var def_src = '.googleadservices.com/pagead/conversion.js';
        var ga = document.createElement ( 'script' );
        ga.type = 'text/javascript'; ga.async = true;
        ga.src = ( 'https:' == document.location.protocol ? 'https://ssl' : 'http://www' ) + def_src;
        var s = document.getElementsByTagName ( 'script' ) [ 0 ];
        s.parentNode.insertBefore ( ga, s );
};

//set global variables with requested values: id and label are MANDATORY and different for each conversion page 
liwe.ga.conversion = function ( params )
{
        var def_url = '//www.googleadservices.com/pagead/conversion/%(id)s/?value=%(value)s&amp;label=%(label)s&amp;guid=ON&amp;script=0';
        var vals = {
                        'id'            : 0,
                        'language'      : 'it',
                        'format'        : '3',
                        'color'         : 'ffffff',
                        'label'         : '',
                        'value'         : 0
                        };

        liwe.types.to_dict ( vals );
        liwe.types.to_dict ( params );
        vals.inherits ( params, true );
        delete vals [ 'parent' ];
        vals.iterate ( function ( v, k ) { window [ 'google_conversion_' + k ] = v; } );

        liwe.ga._init_conv ( vals );

        var url = String.formatDict ( def_url , vals );
        var i = document.createElement ( 'img' );
        i.height = '1';
        i.width = '1';
        i.style.borderStyle = 'none';
        i.src = url;
        document.body.appendChild ( i );
};
